package DataStructure.graph;

import DataStructure.linear.Queue;

public class EdgeWeightedDigraph {
    private final int V;
    private int E;
    private Queue<DirectedEdge>[] adj;


    public EdgeWeightedDigraph(int V){
        this.V=V;
        this.E=0;
        this.adj=new Queue[V];
        for (int i = 0; i < adj.length; i++) {
            adj[i]=new Queue<DirectedEdge>();
        }
    }

    public int V(){
        return V;
    }

    public int E(){
        return E;
    }

    public void addEdge(DirectedEdge e){
        int v = e.from();
        adj[v].enqueue(e);
        E++;
    }

    //获取由顶点v指出的所有的边
    public Queue<DirectedEdge> adj(int v){
        return adj[v];
    }

    //获取加权有向图的所有边
    public Queue<DirectedEdge> edges(){
        Queue<DirectedEdge> edges = new Queue<>();
        for (int i = 0; i < V; i++) {
            for (DirectedEdge e : adj[i]) {
                edges.enqueue(e);
            }
        }
        return edges;
    }
}
